/**
 * 部门管理初始化
 */
var Refund = {
    id: "RefundTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Refund.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},

        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {
            title: 'reservationsId',
            field: 'reservationsId',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: '50px'
        },
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,},
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,},
        // {title: '化名', field: 'pseudonym', align: 'center', valign: 'middle', sortable: true,},
        {title: '退款金额', field: 'refundMoney', align: 'center', valign: 'middle', sortable: true,},

        {title: '是否退款成功', field: 'isRefund', align: 'center', valign: 'middle', sortable: true, width: '4%',},
        {title: '申请人', field: 'recordPeople', align: 'center', valign: 'middle', sortable: true,},
        {title: '退款人', field: 'refundPeople', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '开单时间', field: 'operationTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
        },
        {
            title: '申请时间', field: 'recordTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {
            title: '退款时间', field: 'refundTime', align: 'center', valign: 'middle', sortable: true, width: '12%',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},

    ]
    return columns;
};

/**
 * 检查是否选中
 */
Refund.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Refund.seItem = selected[0];
        return true;
    }
};

/**
 * 打印
 */
Refund.printt = function () {
    if (this.check()) {
        if(Refund.seItem.isRefund!="是"){
            alert("该单据没有退款成功，不能打印！");
            return;
        }
        var reservationsId=Refund.seItem.reservationsId;
        var customerId=Refund.seItem.customerId;
        var operationTime=Refund.seItem.operationTime;
        var recordTime=Refund.seItem.recordTime;
        var rcor=reservationsId+","+customerId+","+operationTime+","+recordTime
        var index = layer.open({
            type: 2,
            title: '打印',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/to_printt/' +  rcor // 传入选中行的客户预约ID
        });
        this.layerIndex = index;
    }
}


Refund.closes = function () {
    // parent.layer.close(window.parent.Customer.layerIndex);
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    $.ajax({
        url: Feng.ctxPath + "/refund/closesBalance",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "operationTime": operationTime
        },
        success: function (data) {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

Refund.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/refund/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            Refund.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};

Refund.refund = function () {
    if (this.check()) {
        if (Refund.seItem.isRefund == "是") {
            alert("已退款！");
            return;
        }
        if (confirm('确定要执行此操作吗?')) {
            $.ajax({
                url: Feng.ctxPath + "/refund/getChangeRefund",
                type: "post",
                data: {
                    "customerId": Refund.seItem.customerId,
                    "reservationsId": Refund.seItem.reservationsId,
                    "operationTime": Refund.seItem.operationTime,
                    "id": Refund.seItem.id
                },
                success: function (data) {
                    Refund.table.refresh({query: Refund.formParams()});
                },
                error: function (data) {
                    alert('内部故障!');
                }
            });
            return true;
        }
        return false;
    }
};

Refund.refunds = function () {
    if (this.check()) {
        var reservationsId = Refund.seItem.reservationsId;
        var customerId = Refund.seItem.customerId;
        var operationTime = Refund.seItem.operationTime;
        var id = Refund.seItem.id;
        var rcoi = reservationsId + "," + customerId + "," + id;
        var index = layer.open({
            type: 2,
            title: '选择退款方式',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/refund/to_refundMethod/' + rcoi
        });
        this.layerIndex = index;
    }
};

Refund.addRefund = function () {
// <#sortOpenRefundList  id="sortOpenRefundList" list="${openBillList}"/>
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    var refundsType = $("#refundsType").val();
    var totleFoldingPay = $('#totleFoldingPay').val();
    var refundMoney = $('#refundMoney').val();
    var remarks = $('#remarks').val();
    var totleServiceActualPay = $('#totleServiceActualPay').val();
    if (refundMoney == null || refundMoney == "") {
        alert("请输入退款金额！");
        return
    }
    if (remarks == null || remarks == "") {
        alert("请输入备注！");
        return
    }
    if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(refundMoney))) {
        alert("请输入正确金额！");
        return;
    }
    if (parseFloat(totleServiceActualPay) < parseFloat(refundMoney)) {
        alert("退款金额不能大付款金額！");
        return
    }
    $.ajax({
        url: Feng.ctxPath + "/refund/addRefund",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "operationTime": operationTime,
            "remarks": remarks,
            "refundsType": refundsType,
            "refundMoney": refundMoney
        },
        success: function (data) {
            Feng.success("退款成功!");
            Refund.closes();
            $('#cancel', parent.document).click();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

Refund.close = function () {
    // parent.layer.close(window.parent.Customer.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

Refund.refundOut = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    var recordTime = $("#recordTime").val();
    var refundMoney = $("#refundMoney").val();
    var total = $("#total").val();
    if (parseFloat(total) < parseFloat(refundMoney)) {
        alert("结账未结束！请继续");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/refund/refundOut",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "operationTime": operationTime,
            "recordTime": recordTime
        },
        success: function (data) {
            Feng.success("退款成功！");
            window.parent.Refund.table.refresh();
            Refund.close();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

Refund.addBalances = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var refundId = $("#refundId").val();
    var amountOfBalance = $("#amountOfBalance").val();
    var checkoutMethod = $("#checkoutMethod").val();
    var remarks = $("#remarks").val();
    var refundMoney = $("#refundMoney").val();
    var total = window.parent.parent.document.getElementById("total").value;
    if (amountOfBalance == null || amountOfBalance == "") {
        alert("请输入结账金额！");
        return;
    }
    if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(amountOfBalance))) {
        alert("请输入正确金额！");
        return;
    }
    if (parseFloat(amountOfBalance) > parseFloat(refundMoney) - parseFloat(total)) {
        alert("退款金额总额不应大于退款总金额！");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/refund/addBalances",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "remarks": remarks,
            "refundId": refundId,
            "checkoutMethod": checkoutMethod,
            "amountOfBalance": amountOfBalance
        },
        success: function (data) {
            window.parent.parent.document.getElementById("total").value = parseFloat(amountOfBalance) + parseFloat(total);
            window.parent.parent.document.getElementById("refundMethodDetails").innerHTML +=
                ('<div class="row">&nbsp&nbsp退款方式：' + checkoutMethod + '&nbsp&nbsp退款金额：' + amountOfBalance + '</div>' +
                    '<div class="row">&nbsp&nbsp备注：' + remarks + '</div>')
            Feng.success("添加结账成功！");
            $('#cancel', parent.document).click();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

/**
 * 查询预约客户列表
 */
Refund.search = function () {
    Refund.table.refresh({query: Refund.formParams()});
};

Refund.choiceRefundMethod = function () {
    var reservationsId = $('#reservationsId').val();
    var customerId = $('#customerId').val();
    var id = $('#id').val();
    var rci = reservationsId + "," + customerId + "," + id;
    var index = layer.open({
        type: 2,
        title: '选择退款方式',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/refund/to_choiceRefundMethod/' + rci
    });
    this.layerIndex = index;
};

Refund.resetSearch = function () {
    $("#customerName").val("");
    $("#customerPhone").val("");
    $("#startRecordTime").val("");
    $("#endRecordTime").val("");
    $("#isRefund").val("");
    $("#oneChannel").val("");
    $("#twoChannel").val("");
    $("#thrChannel").val("");
    Refund.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Refund.formParams = function () {
    var queryData = {};
    queryData['customerName'] = $("#customerName").val();
    queryData['customerPhone'] = $("#customerPhone").val();
    queryData['startRecordTime'] = $("#startRecordTime").val();
    queryData['endRecordTime'] = $("#endRecordTime").val();
    queryData['isRefund'] = $("#isRefund").val();
    queryData['oneChannel'] = $("#oneChannel").val();
    queryData['twoChannel'] = $("#twoChannel").val();
    queryData['thrChannel'] = $("#thrChannel").val();
    return queryData;
}

$(function () {
    var myDate = new Date();
    var month;
    var day;
    if ((myDate.getMonth() + 1) < 10) {
        month = "0" + (myDate.getMonth() + 1);
    } else {
        month = "" + (myDate.getMonth() + 1);
    }
    if (myDate.getDate() < 10) {
        day = "0" + myDate.getDate();
    } else {
        day = "" + myDate.getDate();
    }
    document.getElementById("startRecordTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endRecordTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = Refund.initColumn();
    var table = new BSTable(Refund.id, "/refund/refundList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Refund.formParams());
    Refund.table = table.init();
    Refund.search();
});
